package 极客算法训练营.chapter05;

import java.util.*;

public class 字幕异位词分组 {
    public static void main(String[] args) {
        String s = "adsc";
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        String result1 = chars.toString();
        String result2 = new String(chars);
        System.out.println(chars);
        System.out.println(result1);
        System.out.println("====================");
        System.out.println(result2);
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] array = str.toCharArray();
            Arrays.sort(array);
//            String newStr = array.toString();
            String newStr = new String(array);
            List<String> value = map.getOrDefault(newStr, new ArrayList<>());
            value.add(str);
            map.put(newStr, value);
        }
        return new ArrayList<List<String>>(map.values());
    }

    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            Map<String, List<String>> map = new HashMap<String, List<String>>();
            for (String str : strs) {
                char[] array = str.toCharArray();
                Arrays.sort(array);
                String key = new String(array);
                List<String> list = map.getOrDefault(key, new ArrayList<String>());
                list.add(str);
                map.put(key, list);
            }
            return new ArrayList<List<String>>(map.values());
        }
    }
}
